﻿using BUGCOME.Infrastructure.Attribute;
using BUGCOME.Model.Common;
using BUGCOME.Service.Common.IServices;
using BUGCOME.ServiceCore;

namespace BUGCOME.Service.Common
{
    /// <summary>
    /// 参数配置Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ISysConfigService), ServiceLifetime = LifeTime.Transient)]
    public class SysConfigService : BaseService<SysConfig>, ISysConfigService
    {
        public SysConfig GetSysConfigByKey(string key)
        {
            return Queryable().First(f => f.ConfigKey == key);
        }
        #region 业务逻辑代码

        /// <summary>
        /// 根据key获取系统配置
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<SysConfig> GetSysConfigByKeyAsync(string key)
        {
            return await Queryable().FirstAsync(f => f.ConfigKey == key);
        }

        /// <summary>
        /// 异步新增系统配置记录
        /// </summary>
        /// <param name="entity">待新增的系统配置实体</param>
        /// <returns>新增成功的记录数（Task包装的int结果）</returns>
        public async Task<int> InsertSysConfigAsync(SysConfig entity)
        {
            // 调用异步插入方法，指定需要插入的字段，使用await等待异步操作完成
            return await InsertAsync(entity, it => new
            {
                it.ConfigName,
                it.ConfigKey,
                it.ConfigValue,
                it.ConfigType,
                it.CreateBy,
                it.CreateTime,
                it.UpdateBy,
                it.UpdateTime,
                it.Remark,
            });
        }

        #endregion
    }
}
